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

+3
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

+5
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

+6
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

+3
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

+1
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

+1
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

+7
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
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

+4-18
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

+4-10
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;

lib/CodeGen/SelectionDAG/FastISel.cpp

+6-18
Original file line numberDiff line numberDiff line change
@@ -1454,24 +1454,12 @@ bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) {
14541454
TII.get(TargetOpcode::DBG_LABEL)).addMetadata(DI->getLabel());
14551455
return true;
14561456
}
1457-
case Intrinsic::objectsize: {
1458-
ConstantInt *CI = cast<ConstantInt>(II->getArgOperand(1));
1459-
unsigned long long Res = CI->isZero() ? -1ULL : 0;
1460-
Constant *ResCI = ConstantInt::get(II->getType(), Res);
1461-
unsigned ResultReg = getRegForValue(ResCI);
1462-
if (!ResultReg)
1463-
return false;
1464-
updateValueMap(II, ResultReg);
1465-
return true;
1466-
}
1467-
case Intrinsic::is_constant: {
1468-
Constant *ResCI = ConstantInt::get(II->getType(), 0);
1469-
unsigned ResultReg = getRegForValue(ResCI);
1470-
if (!ResultReg)
1471-
return false;
1472-
updateValueMap(II, ResultReg);
1473-
return true;
1474-
}
1457+
case Intrinsic::objectsize:
1458+
llvm_unreachable("llvm.objectsize.* should have been lowered already");
1459+
1460+
case Intrinsic::is_constant:
1461+
llvm_unreachable("llvm.is.constant.* should have been lowered already");
1462+
14751463
case Intrinsic::launder_invariant_group:
14761464
case Intrinsic::strip_invariant_group:
14771465
case Intrinsic::expect: {

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

+3-21
Original file line numberDiff line numberDiff line change
@@ -6388,29 +6388,11 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
63886388
DAG.setRoot(Res);
63896389
return;
63906390
}
6391-
case Intrinsic::objectsize: {
6392-
// If we don't know by now, we're never going to know.
6393-
ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
6394-
6395-
assert(CI && "Non-constant type in __builtin_object_size?");
6396-
6397-
SDValue Arg = getValue(I.getCalledValue());
6398-
EVT Ty = Arg.getValueType();
6399-
6400-
if (CI->isZero())
6401-
Res = DAG.getConstant(-1ULL, sdl, Ty);
6402-
else
6403-
Res = DAG.getConstant(0, sdl, Ty);
6404-
6405-
setValue(&I, Res);
6406-
return;
6407-
}
6391+
case Intrinsic::objectsize:
6392+
llvm_unreachable("llvm.objectsize.* should have been lowered already");
64086393

64096394
case Intrinsic::is_constant:
6410-
// If this wasn't constant-folded away by now, then it's not a
6411-
// constant.
6412-
setValue(&I, DAG.getConstant(0, sdl, MVT::i1));
6413-
return;
6395+
llvm_unreachable("llvm.is.constant.* should have been lowered already");
64146396

64156397
case Intrinsic::annotation:
64166398
case Intrinsic::ptr_annotation:

lib/CodeGen/TargetPassConfig.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ void TargetPassConfig::addIRPasses() {
657657
// TODO: add a pass insertion point here
658658
addPass(createGCLoweringPass());
659659
addPass(createShadowStackGCLoweringPass());
660+
addPass(createLowerConstantIntrinsicsPass());
660661

661662
// Make sure that no unreachable blocks are instruction selected.
662663
addPass(createUnreachableBlockEliminationPass());

lib/Passes/PassBuilder.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
#include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h"
143143
#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
144144
#include "llvm/Transforms/Scalar/LowerAtomic.h"
145+
#include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h"
145146
#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
146147
#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
147148
#include "llvm/Transforms/Scalar/LowerWidenableCondition.h"
@@ -891,6 +892,8 @@ ModulePassManager PassBuilder::buildModuleOptimizationPipeline(
891892

892893
FunctionPassManager OptimizePM(DebugLogging);
893894
OptimizePM.addPass(Float2IntPass());
895+
OptimizePM.addPass(LowerConstantIntrinsicsPass());
896+
894897
// FIXME: We need to run some loop optimizations to re-rotate loops after
895898
// simplify-cfg and others undo their rotation.
896899

lib/Passes/PassRegistry.def

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ FUNCTION_PASS("libcalls-shrinkwrap", LibCallsShrinkWrapPass())
187187
FUNCTION_PASS("loweratomic", LowerAtomicPass())
188188
FUNCTION_PASS("lower-expect", LowerExpectIntrinsicPass())
189189
FUNCTION_PASS("lower-guard-intrinsic", LowerGuardIntrinsicPass())
190+
FUNCTION_PASS("lower-constant-intrinsics", LowerConstantIntrinsicsPass())
190191
FUNCTION_PASS("lower-widenable-condition", LowerWidenableConditionPass())
191192
FUNCTION_PASS("guard-widening", GuardWideningPass())
192193
FUNCTION_PASS("gvn", GVN())

lib/Transforms/IPO/PassManagerBuilder.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ void PassManagerBuilder::populateModulePassManager(
654654
MPM.add(createGlobalsAAWrapperPass());
655655

656656
MPM.add(createFloat2IntPass());
657+
MPM.add(createLowerConstantIntrinsicsPass());
657658

658659
addExtensionsToPM(EP_VectorizerStart, MPM);
659660

lib/Transforms/Scalar/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ add_llvm_library(LLVMScalarOpts
4444
LoopUnswitch.cpp
4545
LoopVersioningLICM.cpp
4646
LowerAtomic.cpp
47+
LowerConstantIntrinsics.cpp
4748
LowerExpectIntrinsic.cpp
4849
LowerGuardIntrinsic.cpp
4950
LowerWidenableCondition.cpp

0 commit comments

Comments
 (0)