Skip to content

Commit 2c42006

Browse files
authored
Add support for the llvm.is.constant.* intrinsics. (#185)
Add support for the llvm.is.constant.* intrinsics. According to the LLVM LangRef, this intrinsic will never return true when constant folding is not run, so always returning false is safe. Partially resolves issue #149.
1 parent a63302b commit 2c42006

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/Target/CBackend/CBackend.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -2573,6 +2573,7 @@ void CWriter::generateHeader(Module &M) {
25732573
case Intrinsic::trunc:
25742574
case Intrinsic::umax:
25752575
case Intrinsic::umin:
2576+
case Intrinsic::is_constant:
25762577
intrinsicsToDefine.push_back(&*I);
25772578
continue;
25782579
}
@@ -4643,6 +4644,9 @@ void CWriter::printIntrinsicDefinition(FunctionType *funT, unsigned Opcode,
46434644
case Intrinsic::umin:
46444645
Out << " r = a < b ? a : b;\n";
46454646
break;
4647+
case Intrinsic::is_constant:
4648+
Out << " r = 0 /* llvm.is.constant */;\n";
4649+
break;
46464650
}
46474651

46484652
} else {
@@ -4764,6 +4768,7 @@ bool CWriter::lowerIntrinsics(Function &F) {
47644768
case Intrinsic::dbg_declare:
47654769
case Intrinsic::umax:
47664770
case Intrinsic::umin:
4771+
case Intrinsic::is_constant:
47674772
// We directly implement these intrinsics
47684773
break;
47694774

@@ -5078,6 +5083,7 @@ bool CWriter::visitBuiltinCall(CallInst &I, Intrinsic::ID ID) {
50785083
case Intrinsic::trap:
50795084
case Intrinsic::umax:
50805085
case Intrinsic::umin:
5086+
case Intrinsic::is_constant:
50815087
return false; // these use the normal function call emission
50825088
}
50835089
}

test/cpp_tests/test_is_constant.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int f(int i) {
2+
if (__builtin_constant_p(i) &&
3+
i == 1) /* generates the llvm.is.constant.i32 intrinsic at -O0 */
4+
return i + 5;
5+
return 6;
6+
}
7+
8+
int main() { return f(1); }

0 commit comments

Comments
 (0)