llvm.org GIT mirror llvm / c2e77df
Add support for phi nodes in the LLVM C API test Summary: This required to add binding to Instruction::removeFromParent so that instruction can be forward declared and then moved at the right place. Reviewers: bogner, chandlerc, echristo, dblaikie, joker.eph, Wallbraker Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D17057 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@260597 91177308-0d34-0410-b5e6-96231b3b80d8 Amaury Sechet 4 years ago
4 changed file(s) with 69 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
23312331 * Remove and delete an instruction.
23322332 *
23332333 * The instruction specified is removed from its containing building
2334 * block but is kept alive.
2335 *
2336 * @see llvm::Instruction::removeFromParent()
2337 */
2338 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst);
2339
2340 /**
2341 * Remove and delete an instruction.
2342 *
2343 * The instruction specified is removed from its containing building
23342344 * block and then deleted.
23352345 *
23362346 * @see llvm::Instruction::eraseFromParent()
20042004 return wrap(&*--I);
20052005 }
20062006
2007 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2008 unwrap(Inst)->removeFromParent();
2009 }
2010
20072011 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
20082012 unwrap(Inst)->eraseFromParent();
20092013 }
8989 next9:
9090 ret i32 0
9191 }
92
93 define i32 @loop(i32 %i) {
94 br label %cond
95 cond:
96 %c = phi i32 [ %i, %0 ], [ %j, %do ]
97 %p = phi i32 [ %r, %do ], [ 789, %0 ]
98 %1 = icmp eq i32 %c, 0
99 br i1 %1, label %do, label %done
100 do:
101 %2 = sub i32 %p, 23
102 %j = sub i32 %i, 1
103 %r = mul i32 %2, 3
104 br label %cond
105 done:
106 ret i32 %p
107 }
205205 }
206206
207207 // Function argument should always be in the map already.
208 if (LLVMIsAArgument(Src)) {
209 auto i = VMap.find(Src);
210 if (i != VMap.end())
211 return i->second;
212 }
213
214 if (LLVMIsAInstruction(Src)) {
215 auto Ctx = LLVMGetModuleContext(M);
216 auto Builder = LLVMCreateBuilderInContext(Ctx);
217 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
218 LLVMPositionBuilderAtEnd(Builder, BB);
219 auto Dst = CloneInstruction(Src, Builder);
220 LLVMDisposeBuilder(Builder);
221 return Dst;
222 }
223
224 fprintf(stderr, "Could not determine the type of %s\n", Name);
225 exit(-1);
208 auto i = VMap.find(Src);
209 if (i != VMap.end())
210 return i->second;
211
212 if (!LLVMIsAInstruction(Src))
213 report_fatal_error("Expected an instruction");
214
215 auto Ctx = LLVMGetModuleContext(M);
216 auto Builder = LLVMCreateBuilderInContext(Ctx);
217 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
218 LLVMPositionBuilderAtEnd(Builder, BB);
219 auto Dst = CloneInstruction(Src, Builder);
220 LLVMDisposeBuilder(Builder);
221 return Dst;
226222 }
227223
228224 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
233229 // Check if this is something we already computed.
234230 {
235231 auto i = VMap.find(Src);
236 if (i != VMap.end())
237 return i->second;
232 if (i != VMap.end()) {
233 // If we have a hit, it means we already generated the instruction
234 // as a dependancy to somethign else. We need to make sure
235 // it is ordered properly.
236 auto I = i->second;
237 LLVMInstructionRemoveFromParent(I);
238 LLVMInsertIntoBuilderWithName(Builder, I, Name);
239 return I;
240 }
238241 }
239242
240243 // We tried everything, it must be an instruction
363366 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
364367 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
365368 break;
369 }
370 case LLVMPHI: {
371 // We need to agressively set things here because of loops.
372 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
373
374 SmallVector Values;
375 SmallVector Blocks;
376
377 unsigned IncomingCount = LLVMCountIncoming(Src);
378 for (unsigned i = 0; i < IncomingCount; ++i) {
379 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
380 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
381 }
382
383 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
384 return Dst;
366385 }
367386 case LLVMCall: {
368387 SmallVector Args;