Skip to content

Commit 34a481c

Browse files
vladimirradosavljevicakiramenai
authored andcommitted
[EraVM] Only generate GEPs that dominate latch BB in EraVMIndexedMemOpsPrepare
In case we generate GEP that doesn't dominate latch BB, we will run into an issue and verifier will complain: ``` Instruction does not dominate all uses! %3 = getelementptr inbounds i256, ptr addrspace(1) %0, i256 1 %0 = phi ptr addrspace(1) [ %arg3, %bb1 ], [ %3, %bb5 ] ``` This patch fixes this issue. Signed-off-by: Vladimir Radosavljevic <[email protected]>
1 parent 3c47fc4 commit 34a481c

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

llvm/lib/Target/EraVM/EraVMIndexedMemOpsPrepare.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include "llvm/Analysis/LoopPass.h"
7272
#include "llvm/Analysis/ScalarEvolution.h"
7373
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
74+
#include "llvm/IR/Dominators.h"
7475
#include "llvm/IR/IRBuilder.h"
7576
#include "llvm/InitializePasses.h"
7677
#include "llvm/Support/CommandLine.h"
@@ -85,7 +86,7 @@ using namespace llvm;
8586
namespace {
8687

8788
class EraVMIndexedMemOpsPrepare : public LoopPass {
88-
89+
DominatorTree *DT = nullptr;
8990
ScalarEvolution *SE = nullptr;
9091
LLVMContext *Ctx = nullptr;
9192
Loop *CurrentLoop = nullptr;
@@ -102,8 +103,10 @@ class EraVMIndexedMemOpsPrepare : public LoopPass {
102103
}
103104

104105
void getAnalysisUsage(AnalysisUsage &AU) const override {
106+
AU.addRequired<DominatorTreeWrapperPass>();
105107
AU.addRequired<ScalarEvolutionWrapperPass>();
106108
AU.addRequired<LoopInfoWrapperPass>();
109+
AU.addPreserved<DominatorTreeWrapperPass>();
107110
AU.addPreserved<LoopInfoWrapperPass>();
108111
AU.setPreservesCFG();
109112
}
@@ -208,6 +211,12 @@ bool EraVMIndexedMemOpsPrepare::isValidGEPAndIncByOneCell(
208211
if (!PHI)
209212
return false;
210213

214+
// The PHI node must be in the loop header, and BasePtr BB must dominate
215+
// the latch BB.
216+
if (CurrentLoop->getHeader() != PHI->getParent() ||
217+
!DT->dominates(BasePtr->getParent(), CurrentLoop->getLoopLatch()))
218+
return false;
219+
211220
// In rewriteToFavorIndexedMemOps, new PHI is created with incoming values
212221
// from preheader and latch. Check that this PHI has the same incoming BBs.
213222
return PHI->getNumIncomingValues() == 2 &&
@@ -225,6 +234,7 @@ bool EraVMIndexedMemOpsPrepare::runOnLoop(Loop *L, LPPassManager &) {
225234
return false;
226235

227236
auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
237+
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
228238
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
229239
Ctx = &L->getLoopPreheader()->getContext();
230240
CurrentLoop = L;
@@ -268,6 +278,7 @@ char EraVMIndexedMemOpsPrepare::ID = 0;
268278

269279
INITIALIZE_PASS_BEGIN(EraVMIndexedMemOpsPrepare, DEBUG_TYPE,
270280
ERAVM_PREPARE_INDEXED_MEMOPS_NAME, false, false)
281+
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
271282
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
272283
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
273284
INITIALIZE_PASS_END(EraVMIndexedMemOpsPrepare, DEBUG_TYPE,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
; RUN: llc -O3 -stop-after=verify -compile-twice=false < %s | FileCheck %s
2+
3+
target datalayout = "E-p:256:256-i256:256:256-S32-a:256:256"
4+
target triple = "eravm"
5+
6+
define i256 @test(i256 %arg, ptr addrspace(1) %arg1, ptr addrspace(1) %arg2) {
7+
; CHECK-LABEL: @test(
8+
entry:
9+
%icmp1 = icmp eq i256 %arg, 0
10+
br i1 %icmp1, label %bb7, label %bb1
11+
12+
bb1:
13+
br label %bb2
14+
15+
bb2:
16+
%phi1 = phi i256 [ %add, %bb5 ], [ 0, %bb1 ]
17+
%icmp2 = icmp ugt i256 %phi1, 100
18+
br i1 %icmp2, label %bb3, label %bb4
19+
20+
bb3:
21+
; CHECK: getelementptr inbounds i256, ptr addrspace(1) %arg1, i256 %phi1
22+
%gep1 = getelementptr inbounds i256, ptr addrspace(1) %arg1, i256 %phi1
23+
store i256 5, ptr addrspace(1) %gep1, align 32
24+
br label %bb5
25+
26+
bb4:
27+
; CHECK: getelementptr inbounds i256, ptr addrspace(1) %arg2, i256 %phi1
28+
%gep2 = getelementptr inbounds i256, ptr addrspace(1) %arg2, i256 %phi1
29+
store i256 10, ptr addrspace(1) %gep2, align 32
30+
br label %bb5
31+
32+
bb5:
33+
%add = add i256 %phi1, 1
34+
%cmp3 = icmp eq i256 %add, 0
35+
br i1 %cmp3, label %bb6, label %bb2
36+
37+
bb6:
38+
br label %bb7
39+
40+
bb7:
41+
%phi2 = phi i256 [ 0, %entry ], [ %add, %bb6 ]
42+
ret i256 %phi2
43+
}

0 commit comments

Comments
 (0)