llvm.org GIT mirror llvm / 185aa0e
Add support for memory operations (load/store/gep) in C API echo test Summary: As per title. Reviewers: bogner, chandlerc, echristo, dblaikie, joker.eph, Wallbraker Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D17245 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@261174 91177308-0d34-0410-b5e6-96231b3b80d8 Amaury Sechet 4 years ago
4 changed file(s) with 98 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
25602560 */
25612561
25622562 /**
2563 * @defgroup LLVMCCoreValueInstructionGetElementPointer GEPs
2564 *
2565 * Functions in this group only apply to instructions that map to
2566 * llvm::GetElementPtrInst instances.
2567 *
2568 * @{
2569 */
2570
2571 /**
2572 * Check whether the given GEP instruction is inbounds.
2573 */
2574 LLVMBool LLVMIsInBounds(LLVMValueRef GEP);
2575
2576 /**
2577 * Set the given GEP instruction to be inbounds or not.
2578 */
2579 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool b);
2580
2581 /**
2582 * @}
2583 */
2584
2585 /**
25632586 * @defgroup LLVMCCoreValueInstructionPHINode PHI Nodes
25642587 *
25652588 * Functions in this group only apply to instructions that map to
26052628
26062629 /**
26072630 * Obtain the number of indices.
2631 * NB: This also works on GEP.
26082632 */
26092633 unsigned LLVMGetNumIndices(LLVMValueRef Inst);
26102634
21462146 return wrap(unwrap(Alloca)->getAllocatedType());
21472147 }
21482148
2149 /*--.. Operations on gep instructions (only) ...............................--*/
2150
2151 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2152 return unwrap(GEP)->isInBounds();
2153 }
2154
2155 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool b) {
2156 return unwrap(GEP)->setIsInBounds(b);
2157 }
2158
21492159 /*--.. Operations on phi nodes .............................................--*/
21502160
21512161 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
21712181
21722182 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
21732183 auto *I = unwrap(Inst);
2184 if (auto *GEP = dyn_cast(I))
2185 return GEP->getNumIndices();
21742186 if (auto *EV = dyn_cast(I))
21752187 return EV->getNumIndices();
21762188 if (auto *IV = dyn_cast(I))
0 ; RUN: llvm-as < %s | llvm-dis > %t.orig
1 ; RUN: llvm-as < %s | llvm-c-test --echo > %t.echo
2 ; RUN: diff -w %t.orig %t.echo
3
4 %S = type { i32, i32 }
5
6 define i32 @method(%S* %this, i32 %arg.a, i32 %arg.b) {
7 %a = alloca i32
8 store i32 %arg.a, i32* %a, align 4
9 %b = alloca i32
10 store i32 %arg.b, i32* %b
11 %1 = load i32, i32* %a, align 4
12 %2 = load i32, i32* %b, align 4
13 %3 = add i32 %1, %2
14 %4 = getelementptr inbounds %S, %S* %this, i32 0, i32 0
15 %5 = load i32, i32* %4, align 4
16 %6 = add i32 %3, %5
17 %7 = getelementptr inbounds %S, %S* %this, i32 0, i32 1
18 %8 = load i32, i32* %7, align 4
19 %9 = add i32 %6, %8
20 ret i32 %9
21 }
22
23 define i32 @main() {
24 %s = alloca %S
25 store %S zeroinitializer, %S* %s
26 %1 = getelementptr inbounds %S, %S* %s, i32 0, i32 0
27 %2 = getelementptr inbounds %S, %S* %s, i32 0, i32 1
28 store i32 1, i32* %2
29 store i32 1, i32* %1
30 %3 = insertvalue { %S*, i32 (%S*, i32, i32)* } undef, %S* %s, 0
31 %4 = insertvalue { %S*, i32 (%S*, i32, i32)* } %3, i32 (%S*, i32, i32)* @method, 1
32 %5 = extractvalue { %S*, i32 (%S*, i32, i32)* } %4, 0
33 %6 = extractvalue { %S*, i32 (%S*, i32, i32)* } %4, 1
34 %7 = call i32 %6(%S* %5, i32 38, i32 2)
35 ret i32 %7
36 }
463463 Dst = LLVMBuildAlloca(Builder, Ty, Name);
464464 break;
465465 }
466 case LLVMLoad: {
467 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
468 Dst = LLVMBuildLoad(Builder, Ptr, Name);
469 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
470 break;
471 }
472 case LLVMStore: {
473 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
474 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
475 Dst = LLVMBuildStore(Builder, Val, Ptr);
476 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
477 break;
478 }
479 case LLVMGetElementPtr: {
480 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
481 SmallVector Idx;
482 int NumIdx = LLVMGetNumIndices(Src);
483 for (int i = 1; i <= NumIdx; i++)
484 Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
485 if (LLVMIsInBounds(Src))
486 Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
487 else
488 Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
489 break;
490 }
466491 case LLVMICmp: {
467492 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
468493 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));