[cfe-commits] r157262 - in /cfe/trunk: include/clang/Basic/LangOptions.def include/clang/Frontend/CodeGenOptions.h lib/CodeGen/BackendUtil.cpp lib/CodeGen/CGExpr.cpp lib/CodeGen/CodeGenFunction.cpp lib/Frontend/CompilerInvocation.cpp test/CodeGen/bounds-checking.c

Nuno Lopes nunoplopes at sapo.pt
Tue May 22 10:19:46 PDT 2012


Author: nlopes
Date: Tue May 22 12:19:45 2012
New Revision: 157262

URL: http://llvm.org/viewvc/llvm-project?rev=157262&view=rev
Log:
wire -fbounds-checking to the new LLVM bounds checking pass

Modified:
    cfe/trunk/include/clang/Basic/LangOptions.def
    cfe/trunk/include/clang/Frontend/CodeGenOptions.h
    cfe/trunk/lib/CodeGen/BackendUtil.cpp
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp
    cfe/trunk/test/CodeGen/bounds-checking.c

Modified: cfe/trunk/include/clang/Basic/LangOptions.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/LangOptions.def?rev=157262&r1=157261&r2=157262&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/LangOptions.def (original)
+++ cfe/trunk/include/clang/Basic/LangOptions.def Tue May 22 12:19:45 2012
@@ -157,7 +157,6 @@
                "maximum constexpr call depth")
 BENIGN_LANGOPT(NumLargeByValueCopy, 32, 0, 
         "if non-zero, warn about parameter or return Warn if parameter/return value is larger in bytes than this setting. 0 is no check.")
-BENIGN_LANGOPT(BoundsChecking , 8, 0, "if non-zero, add run-time bounds checking code")
 VALUE_LANGOPT(MSCVersion, 32, 0, 
               "version of Microsoft Visual C/C++")
 

Modified: cfe/trunk/include/clang/Frontend/CodeGenOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/CodeGenOptions.h?rev=157262&r1=157261&r2=157262&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/CodeGenOptions.h (original)
+++ cfe/trunk/include/clang/Frontend/CodeGenOptions.h Tue May 22 12:19:45 2012
@@ -172,6 +172,9 @@
   /// or 0 if unspecified.
   unsigned NumRegisterParameters;
 
+  /// The run-time penalty for bounds checking, or 0 to disable.
+  unsigned char BoundsChecking;
+
 public:
   CodeGenOptions() {
     AsmVerbose = 0;
@@ -224,6 +227,7 @@
     VerifyModule = 1;
     StackRealignment = 0;
     StackAlignment = 0;
+    BoundsChecking = 0;
 
     DebugInfo = NoDebugInfo;
     Inlining = NoInlining;

Modified: cfe/trunk/lib/CodeGen/BackendUtil.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/BackendUtil.cpp?rev=157262&r1=157261&r2=157262&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/BackendUtil.cpp (original)
+++ cfe/trunk/lib/CodeGen/BackendUtil.cpp Tue May 22 12:19:45 2012
@@ -121,6 +121,12 @@
     PM.add(createObjCARCOptPass());
 }
 
+static unsigned BoundsChecking;
+static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
+                                    PassManagerBase &PM) {
+  PM.add(createBoundsCheckingPass(BoundsChecking));
+}
+
 static void addAddressSanitizerPass(const PassManagerBuilder &Builder,
                                     PassManagerBase &PM) {
   PM.add(createAddressSanitizerPass());
@@ -160,6 +166,14 @@
                            addObjCARCOptPass);
   }
 
+  if (CodeGenOpts.BoundsChecking > 0) {
+    BoundsChecking = CodeGenOpts.BoundsChecking;
+    PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
+                           addBoundsCheckingPass);
+    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
+                           addBoundsCheckingPass);
+  }
+
   if (LangOpts.AddressSanitizer) {
     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
                            addAddressSanitizerPass);

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=157262&r1=157261&r2=157262&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Tue May 22 12:19:45 2012
@@ -517,7 +517,7 @@
 }
 
 void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
-  if (BoundsChecking <= 0)
+  if (!CatchUndefined)
     return;
 
   // This needs to be to the standard address space.

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=157262&r1=157261&r2=157262&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Tue May 22 12:19:45 2012
@@ -41,7 +41,6 @@
     CXXVTTValue(0), OutermostConditional(0), TerminateLandingPad(0),
     TerminateHandler(0), TrapBB(0) {
 
-  BoundsChecking = getContext().getLangOpts().BoundsChecking;
   CatchUndefined = getContext().getLangOpts().CatchUndefined;
   CGM.getCXXABI().getMangleContext().startNewFunction();
 }

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=157262&r1=157261&r2=157262&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Tue May 22 12:19:45 2012
@@ -283,6 +283,8 @@
     Res.push_back("-fobjc-dispatch-method=non-legacy");
     break;
   }
+  if (Opts.BoundsChecking > 0)
+    Res.push_back("-fbounds-checking=" + llvm::utostr(Opts.BoundsChecking));
   if (Opts.NumRegisterParameters)
     Res.push_back("-mregparm", llvm::utostr(Opts.NumRegisterParameters));
   if (Opts.NoGlobalMerge)
@@ -675,8 +677,6 @@
     Res.push_back("-fno-operator-names");
   if (Opts.PascalStrings)
     Res.push_back("-fpascal-strings");
-  if (Opts.BoundsChecking > 0)
-    Res.push_back("-fbounds-checking=" + llvm::utostr(Opts.BoundsChecking));
   if (Opts.CatchUndefined)
     Res.push_back("-fcatch-undefined-behavior");
   if (Opts.AddressSanitizer)
@@ -1217,6 +1217,8 @@
   Opts.UnwindTables = Args.hasArg(OPT_munwind_tables);
   Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic");
   Opts.TrapFuncName = Args.getLastArgValue(OPT_ftrap_function_EQ);
+  Opts.BoundsChecking = Args.getLastArgIntValue(OPT_fbounds_checking_EQ, 0,
+                                                Diags);
 
   Opts.FunctionSections = Args.hasArg(OPT_ffunction_sections);
   Opts.DataSections = Args.hasArg(OPT_fdata_sections);
@@ -1955,11 +1957,7 @@
     Opts.ObjCNonFragileABI2 = true;
   Opts.ObjCDefaultSynthProperties =
     Args.hasArg(OPT_fobjc_default_synthesize_properties);
-  Opts.BoundsChecking = 0;
-  if ((Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior)))
-    Opts.BoundsChecking = 5;
-  Opts.BoundsChecking = Args.getLastArgIntValue(OPT_fbounds_checking_EQ,
-                                                Opts.BoundsChecking, Diags);
+  Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
   Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
   Opts.PackStruct = Args.getLastArgIntValue(OPT_fpack_struct_EQ, 0, Diags);
   Opts.PICLevel = Args.getLastArgIntValue(OPT_pic_level, 0, Diags);

Modified: cfe/trunk/test/CodeGen/bounds-checking.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/bounds-checking.c?rev=157262&r1=157261&r2=157262&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/bounds-checking.c (original)
+++ cfe/trunk/test/CodeGen/bounds-checking.c Tue May 22 12:19:45 2012
@@ -3,23 +3,24 @@
 // CHECK: @f
 double f(int b, int i) {
   double a[b];
+  // CHECK: trap
   return a[i];
-  // CHECK: objectsize.i64({{.*}}, i1 false)
-  // CHECK: icmp uge i64 {{.*}}, 8
 }
 
 // CHECK: @f2
 void f2() {
+  // everything is constant; no trap possible
+  // CHECK-NOT: trap
   int a[2];
-  // CHECK: objectsize.i64({{.*}}, i1 false)
-  // CHECK: icmp uge i64 {{.*}}, 4
   a[1] = 42;
   
   short *b = malloc(64);
-  // CHECK: objectsize.i64({{.*}}, i1 false)
-  // CHECK: icmp uge i64 {{.*}}, 4
-  // CHECK: getelementptr {{.*}}, i64 5
-  // CHECK: objectsize.i64({{.*}}, i1 false)
-  // CHECK: icmp uge i64 {{.*}}, 2
-  b[5] = a[1]+2;
+  b[5] = *a + a[1] + 2;
+}
+
+// CHECK: @f3
+void f3() {
+  int a[1];
+  // CHECK: trap
+  a[2] = 1;
 }





More information about the cfe-commits mailing list