Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 732f95f

Browse files
committed
Reapply r374743 with a fix for the ocaml binding
Add a pass to lower is.constant and objectsize intrinsics This pass lowers is.constant and objectsize intrinsics not simplified by earlier constant folding, i.e. if the object given is not constant or if not using the optimized pass chain. The result is recursively simplified and constant conditionals are pruned, so that dead blocks are removed even for -O0. This allows inline asm blocks with operand constraints to work all the time. The new pass replaces the existing lowering in the codegen-prepare pass and fallbacks in SDAG/GlobalISEL and FastISel. The latter now assert on the intrinsics. Differential Revision: https://reviews.llvm.org/D65280 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@374784 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent bd67315 commit 732f95f

39 files changed

+402
-457
lines changed

bindings/ocaml/transforms/scalar_opts/llvm_scalar_opts.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ external add_early_cse
114114
external add_lower_expect_intrinsic
115115
: [< Llvm.PassManager.any ] Llvm.PassManager.t -> unit
116116
= "llvm_add_lower_expect_intrinsic"
117+
external add_lower_constant_intrinsics
118+
: [< Llvm.PassManager.any ] Llvm.PassManager.t -> unit
119+
= "llvm_add_lower_constant_intrinsics"
117120
external add_type_based_alias_analysis
118121
: [< Llvm.PassManager.any ] Llvm.PassManager.t -> unit
119122
= "llvm_add_type_based_alias_analysis"

bindings/ocaml/transforms/scalar_opts/llvm_scalar_opts.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ external add_lower_expect_intrinsic
191191
: [< Llvm.PassManager.any ] Llvm.PassManager.t -> unit
192192
= "llvm_add_lower_expect_intrinsic"
193193

194+
(** See the [llvm::createLowerConstantIntrinsicsPass] function. *)
195+
external add_lower_constant_intrinsics
196+
: [< Llvm.PassManager.any ] Llvm.PassManager.t -> unit
197+
= "llvm_add_lower_constant_intrinsics"
198+
194199
(** See the [llvm::createTypeBasedAliasAnalysisPass] function. *)
195200
external add_type_based_alias_analysis
196201
: [< Llvm.PassManager.any ] Llvm.PassManager.t -> unit

bindings/ocaml/transforms/scalar_opts/scalar_opts_ocaml.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ CAMLprim value llvm_add_lower_expect_intrinsic(LLVMPassManagerRef PM) {
236236
return Val_unit;
237237
}
238238

239+
/* [<Llvm.PassManager.any] Llvm.PassManager.t -> unit */
240+
CAMLprim value llvm_add_lower_constant_intrinsics(LLVMPassManagerRef PM) {
241+
LLVMAddLowerConstantIntrinsicsPass(PM);
242+
return Val_unit;
243+
}
244+
239245
/* [<Llvm.PassManager.any] Llvm.PassManager.t -> unit */
240246
CAMLprim value llvm_add_type_based_alias_analysis(LLVMPassManagerRef PM) {
241247
LLVMAddTypeBasedAliasAnalysisPass(PM);

include/llvm-c/Transforms/Scalar.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ void LLVMAddEarlyCSEMemSSAPass(LLVMPassManagerRef PM);
147147
/** See llvm::createLowerExpectIntrinsicPass function */
148148
void LLVMAddLowerExpectIntrinsicPass(LLVMPassManagerRef PM);
149149

150+
/** See llvm::createLowerConstantIntrinsicsPass function */
151+
void LLVMAddLowerConstantIntrinsicsPass(LLVMPassManagerRef PM);
152+
150153
/** See llvm::createTypeBasedAliasAnalysisPass function */
151154
void LLVMAddTypeBasedAliasAnalysisPass(LLVMPassManagerRef PM);
152155

include/llvm/InitializePasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ void initializeLoopVectorizePass(PassRegistry&);
243243
void initializeLoopVersioningLICMPass(PassRegistry&);
244244
void initializeLoopVersioningPassPass(PassRegistry&);
245245
void initializeLowerAtomicLegacyPassPass(PassRegistry&);
246+
void initializeLowerConstantIntrinsicsPass(PassRegistry&);
246247
void initializeLowerEmuTLSPass(PassRegistry&);
247248
void initializeLowerExpectIntrinsicPass(PassRegistry&);
248249
void initializeLowerGuardIntrinsicLegacyPassPass(PassRegistry&);

include/llvm/LinkAllPasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ namespace {
140140
(void) llvm::createLoopVersioningLICMPass();
141141
(void) llvm::createLoopIdiomPass();
142142
(void) llvm::createLoopRotatePass();
143+
(void) llvm::createLowerConstantIntrinsicsPass();
143144
(void) llvm::createLowerExpectIntrinsicPass();
144145
(void) llvm::createLowerInvokePass();
145146
(void) llvm::createLowerSwitchPass();

include/llvm/Transforms/Scalar.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,13 @@ extern char &InferAddressSpacesID;
395395
// "block_weights" metadata.
396396
FunctionPass *createLowerExpectIntrinsicPass();
397397

398+
//===----------------------------------------------------------------------===//
399+
//
400+
// LowerConstantIntrinsicss - Expand any remaining llvm.objectsize and
401+
// llvm.is.constant intrinsic calls, even for the unknown cases.
402+
//
403+
FunctionPass *createLowerConstantIntrinsicsPass();
404+
398405
//===----------------------------------------------------------------------===//
399406
//
400407
// PartiallyInlineLibCalls - Tries to inline the fast path of library
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===- LowerConstantIntrinsics.h - Lower constant int. pass -*- C++ -*-========//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
///
10+
/// The header file for the LowerConstantIntrinsics pass as used by the new pass
11+
/// manager.
12+
///
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_TRANSFORMS_SCALAR_LOWERCONSTANTINTRINSICS_H
16+
#define LLVM_TRANSFORMS_SCALAR_LOWERCONSTANTINTRINSICS_H
17+
18+
#include "llvm/IR/Function.h"
19+
#include "llvm/IR/PassManager.h"
20+
21+
namespace llvm {
22+
23+
struct LowerConstantIntrinsicsPass :
24+
PassInfoMixin<LowerConstantIntrinsicsPass> {
25+
public:
26+
explicit LowerConstantIntrinsicsPass() {}
27+
28+
/// Run the pass over the function.
29+
///
30+
/// This will lower all remaining 'objectsize' and 'is.constant'`
31+
/// intrinsic calls in this function, even when the argument has no known
32+
/// size or is not a constant respectively. The resulting constant is
33+
/// propagated and conditional branches are resolved where possible.
34+
/// This complements the Instruction Simplification and
35+
/// Instruction Combination passes of the optimized pass chain.
36+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &);
37+
};
38+
39+
}
40+
41+
#endif

lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,24 +1868,10 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, bool &ModifiedDT) {
18681868
});
18691869
return true;
18701870
}
1871-
case Intrinsic::objectsize: {
1872-
// Lower all uses of llvm.objectsize.*
1873-
Value *RetVal =
1874-
lowerObjectSizeCall(II, *DL, TLInfo, /*MustSucceed=*/true);
1875-
1876-
resetIteratorIfInvalidatedWhileCalling(BB, [&]() {
1877-
replaceAndRecursivelySimplify(CI, RetVal, TLInfo, nullptr);
1878-
});
1879-
return true;
1880-
}
1881-
case Intrinsic::is_constant: {
1882-
// If is_constant hasn't folded away yet, lower it to false now.
1883-
Constant *RetVal = ConstantInt::get(II->getType(), 0);
1884-
resetIteratorIfInvalidatedWhileCalling(BB, [&]() {
1885-
replaceAndRecursivelySimplify(CI, RetVal, TLInfo, nullptr);
1886-
});
1887-
return true;
1888-
}
1871+
case Intrinsic::objectsize:
1872+
llvm_unreachable("llvm.objectsize.* should have been lowered already");
1873+
case Intrinsic::is_constant:
1874+
llvm_unreachable("llvm.is.constant.* should have been lowered already");
18891875
case Intrinsic::aarch64_stlxr:
18901876
case Intrinsic::aarch64_stxr: {
18911877
ZExtInst *ExtVal = dyn_cast<ZExtInst>(CI->getArgOperand(0));

lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,18 +1437,12 @@ bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
14371437
MIRBuilder.buildConstant(Reg, TypeID);
14381438
return true;
14391439
}
1440-
case Intrinsic::objectsize: {
1441-
// If we don't know by now, we're never going to know.
1442-
const ConstantInt *Min = cast<ConstantInt>(CI.getArgOperand(1));
1440+
case Intrinsic::objectsize:
1441+
llvm_unreachable("llvm.objectsize.* should have been lowered already");
14431442

1444-
MIRBuilder.buildConstant(getOrCreateVReg(CI), Min->isZero() ? -1ULL : 0);
1445-
return true;
1446-
}
14471443
case Intrinsic::is_constant:
1448-
// If this wasn't constant-folded away by now, then it's not a
1449-
// constant.
1450-
MIRBuilder.buildConstant(getOrCreateVReg(CI), 0);
1451-
return true;
1444+
llvm_unreachable("llvm.is.constant.* should have been lowered already");
1445+
14521446
case Intrinsic::stackguard:
14531447
getStackGuard(getOrCreateVReg(CI), MIRBuilder);
14541448
return true;

0 commit comments

Comments
 (0)